package com.handu.luna.core.domain.user;


import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.handu.luna.core.domain.BaseRepository;
import com.handu.luna.core.domain.user.User.UserStatus;


public interface UserRepository extends JpaRepository<User,Long>, BaseRepository<User>,JpaSpecificationExecutor<User>{
	
	@Query("from User u  order by id desc")
	public Page<User> findAll(Pageable pageable);

	public List<User> findByStatus(UserStatus status);
	
	public List<User> findByNameAndPassword(String name,String password);
	
	public List<User> findByName(String name);
	
	public List<User> findByEmail(String email);
	
	public User findByTokenSalt(String tokenSalt);
	
	public List<User> findByMobile(String mobile);
	
	public List<User> findByMobileAndPassword(String mobile,String password);
	
	public List<User> findByEmailAndPassword(String email,String password);
	
	@Query("from User u  where u.name = :name AND u.id <> :id")
	public List<User> findByNameAndId(@Param("name") String name,@Param("id") Long id);
	
	@Query("from User u  where u.email = :email AND u.id <> :id")
	public List<User> findByEmailAndId(@Param("email") String email,@Param("id") Long id);
	
	@Query("from User u  where u.mobile = :mobile AND u.id <> :id")
	public List<User> findByMobileAndId(@Param("mobile") String mobile,@Param("id") Long id);

    @Query("select u from User u where u.levelPoint >= ?1 and u.levelPoint <= ?2 and (u.userRank = '0' or u.userRank is null)")
    public List<User> findbyLevelPoint(int minRank,int maxRank);

    @Query("select u from User u where u.userRank = ?1")
    public List<User> findByUserRank(String userRankId);

    @Query("select u from User u where u.name like ?1")
    public List<User> findByNameLike(String name);
    
    @Modifying
    @Query("update User u set u.levelPoint = levelPoint + :levelPoint where u.id = :id")
    public void IncreaselLevelPointById(@Param("id") long id,@Param("levelPoint") int levelPoint);
    
    @Modifying
    @Query("update User u set u.levelPoint = levelPoint - :levelPoint where u.id = :id")
    public void ReduceLevelPointById(@Param("id") long id,@Param("levelPoint") int levelPoint);
    
    @Modifying
    @Query("update User u set u.consumePoint = consumePoint + :consumePoint where u.id = :id")
    public void IncreaselConsumePointById(@Param("id") long id,@Param("consumePoint") int consumePoint);
    
    @Modifying
    @Query("update User u set u.consumePoint = consumePoint - :consumePoint where u.id = :id")
    public void ReduceConsumePointById(@Param("id") long id,@Param("consumePoint") int consumePoint);

    /****************************************商品详情页****************************************************/

    /*****************************************************************************************************/
    
    
}